package com.vision.util.http.param;

import com.google.common.collect.Maps;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.IOException;
import java.util.Map;

/**
 * @author zhongchao
 * @title
 * @date 2017-08-22
 * @since v1.0.0
 */
@SuppressWarnings("unchecked")
public abstract class HttpRequestParams<T extends HttpRequestParams> {

    protected String url;

    protected String charset;

    /**
     * header
     */
    protected Map<String, String> headers;

    /**
     * params
     */
    protected Map<String, String> params;

    public HttpRequestParams(String url, String charset) {
        this.charset = charset == null ? "UTF-8" : charset;
        this.url = url;
        headers = Maps.newHashMap();
        params = Maps.newHashMap();
    }

    public T setHeaders(Map<String, String> headers) {
        this.headers = headers;
        return (T) this;
    }

    public T addHeader(String key, String value) {
        this.headers.put(key, value);
        return (T) this;
    }

    public T setParams(Map<String, String> params) {
        this.params = params;
        return (T) this;
    }

    public T addParams(String key, String value) {
        this.params.put(key, value);
        return (T) this;
    }

    public abstract HttpRequestBase build() throws IOException;
}
